﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Raytracererer
{
    class Lens : Primitive
    {
        Vector3 center;
        Vector3 axisOfSymmetry;
        Sphere sphere1;
        Sphere sphere2;

        #region Gneric values and functions
        Color m_color;
        double m_transparency;
        double m_reflectivity;
        double m_refractiveIndex;
        double m_diffuse;
        Color m_specularity;
        public Color color
        {
            get { return m_color; }
            set { m_color = value; }
        }
        public double transparency
        {
            get { return m_transparency; }
            set { m_transparency = value; }
        }
        public double reflectivity
        {
            get;
            set;
        }
        public double refractionIndex
        {
            get { return m_refractiveIndex; }
            set { m_refractiveIndex = value; }
        }
        public double diffuse
        {
            get;
            set;
        }
        public Color specularity
        {
            get;
            set;
        }
        #endregion

        public Lens(Vector3 center, Vector3 axisOfSymmetry)
        {
            refractionIndex = 1.3;
            color = new Color(1);
            this.center = center;
            this.axisOfSymmetry = axisOfSymmetry.Normalized();
            double radius = 0.7;
            sphere1 = new Sphere(radius, center + 0.9 * radius * this.axisOfSymmetry, new Color(0.5));
            sphere2 = new Sphere(radius, center - 0.9 * radius * this.axisOfSymmetry, new Color(0.5));
            transparency = 1;

        }
        public double Intersect(Ray ray, out Vector3 position)
        {
            position = null;
            Vector3 sphere1Intersection;
            Vector3 sphere2Intersection;
            double dist1 = sphere1.Intersect(ray, out sphere1Intersection);
            double dist2 = sphere2.Intersect(ray, out sphere2Intersection);
            var set = new SortedList<double, Vector3>();

            if (dist1 != -1 && sphere2.isIn(sphere1Intersection))
                set.Add(dist1, sphere1Intersection);
            if (dist2 != -1 && sphere1.isIn(sphere2Intersection))
                set.Add(dist2 + 0.0001, sphere2Intersection); //får problem när jag ser den från sidan

            if (set.Count == 0)
                return -1;
            position = set.First().Value;
            return set.First().Key;
        }
        public bool isIn(Vector3 position)
        {
            return sphere1.isIn(position) && sphere2.isIn(position);
        }
        public Vector3 Normal(Vector3 position)
        {
            //visar sig vara totally fucking random och otillräckligt
            if (sphere1.isIn(position) && sphere2.isIn(position))
            {
                if ((position - sphere1.center).LengthSquared() < (position - sphere2.center).LengthSquared())
                    return sphere2.Normal(position);
                else
                    return sphere1.Normal(position);
            }
            if (sphere1.isIn(position))
                return sphere2.Normal(position);
            if (sphere2.isIn(position))
                return sphere1.Normal(position);
            return new Vector3(1, 1, 1);
        }
        public Color ColorAt(Vector3 position)
        {
            return m_color;
        }
    }
}
